﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

namespace logicunivesityinventorystore.Store
{
    public partial class DisbursementDetails : System.Web.UI.Page
    {
        static string listID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
             listID = Request.QueryString["retrievalListID"];
            //foreach (GridViewRow row in gvDisbursementList.Rows)
            //{
            //    RangeValidator rd = (RangeValidator)row.FindControl("RangeValidator1");
            //    rd.MaximumValue = row.Cells[3].Text;
            //    rd.MinimumValue = "0";
 
            //}
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //foreach (GridViewRow gvr in gvDisbursementList.Rows)
            //{
            //    int actualQty = Convert.ToInt32((gvr.FindControl("txtCollectedQty") as TextBox).Text);
            //    int collectedQty = Convert.ToInt32((gvr.Cells[3]).Text);
            //    if (actualQty < collectedQty)
            //    {
            //        (gvr.FindControl("txtCollectedQty") as TextBox).Text = "";
            //    }
            //}
            AdjustmentVoucherFacade adjustment = new AdjustmentVoucherFacade();
            AdjustmentVoucher adjVoucher = new AdjustmentVoucher();
            List<AdjustmentVoucherDetail> avDetails = new List<AdjustmentVoucherDetail>();
            ItemFacade item = new ItemFacade();
            SuppliersFacade supplier = new SuppliersFacade();
            bool status = true;
            float total = 0;
            string clerkID=Session["UserName"].ToString();
            string adID = adjustment.GetAdjustmentID(clerkID);
            foreach (GridViewRow row in gvDisbursementList.Rows)
            {
                
                TextBox tbCollectQty = (TextBox)row.FindControl("txtCollectedQty");
                TextBox tbRemarks = (TextBox)row.FindControl("txtRemarks");
                int collectQty = Convert.ToInt32(tbCollectQty.Text);
                string remarks = tbRemarks.Text;
                RetrivalListFacade retrieval = new RetrivalListFacade();
                string itemCode=row.Cells[0].Text;
                retrieval.UpdateCollectQty(listID, itemCode, collectQty);
                retrieval.UpdateRemark(listID,itemCode,remarks);
                int actualqty=0;
                if (row.Cells[3].Text != "&nbsp;") 
                actualqty = Convert.ToInt32(row.Cells[3].Text);
                if (collectQty < actualqty)
                {
                     int difference = actualqty - collectQty;
                    AdjustmentVoucherDetail adVDetail = new AdjustmentVoucherDetail();
                    double price = supplier.FindPriceByItemCodeAndSupplierID(itemCode, item.FindItemByID(itemCode).Supplier1);
                    adVDetail.ItemCode = itemCode;
                    adVDetail.Operation = "-";
                    adVDetail.AdjustedQty = difference;
                    adVDetail.Reason = remarks;
                    adVDetail.VoucherID = adID;
                    adVDetail.SuggestedPrice= (float)price;
                    total += (float) adVDetail.SuggestedPrice;
                    avDetails.Add(adVDetail);
                    if (status)
                    {
                        adjVoucher.ClerkID = clerkID;
                        adjVoucher.DateIssue = DateTime.Now.Date;
                        adjVoucher.VoucherID = adID;
                        adjVoucher.Status = "Pending";
                        adjVoucher.TotalPrice = total;
                        status = false ;
                        
                    }
            
                    
                   
                }
                gvDisbursementList.DataBind();
 
            }
            adjustment.insertAdjustment(adjVoucher, avDetails);
            ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('Change Successfully!');window.location.href = 'DisbursementDetails.aspx'", true);
        }

        protected void gvDisbursementList_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvDisbursementList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
             
                //RangeValidator rd = (RangeValidator)(e.Row.FindControl("RangeValidator1"));
                //rd.MinimumValue = "0";
                //if (e.Row.Cells[3].Text != "&nbsp;")
                //    rd.MaximumValue = e.Row.Cells[3].Text;
                //else
                //    rd.MaximumValue = "10";               

            }
        }
    }
}